<?php
/**************************************************************************************
* Class: my_pager
* Methods:
* vi_tri
* so_trang
* xuat_phan_trang
* xuat_phan_trang_hinh
* Redistribute as you see fit.
**************************************************************************************/
class my_pager
{
/***********************************************************************************
* Ham int vi_tri (int so_luong)
* Tra ve dong bat dau cua trang duoc chon dua tren trang lay duoc va bien so_luong
***********************************************************************************/
	function vi_tri($so_luong)
	{
		if ((!isset($_GET['page'])) || ($_GET['page'] == "1"))
		{
			$start = 0;
			$_GET['page'] = 1;
		}
		else
		{
			$start = ($_GET['page']-1) * $so_luong;
		}
		
		return $start;
	}
/***********************************************************************************
* Ham int so_trang (int tong_so_sp, int so_luong)
* Tra ve so luong trang can thiet dua tren tong so dong co trong table va so_luong
***********************************************************************************/
	function tong_so_trang($tong_so_sp, $so_luong)
	{
		$tong_so_trang = (($tong_so_sp % $so_luong) == 0) ? $tong_so_sp / $so_luong : floor($tong_so_sp / $so_luong) + 1;
		return $tong_so_trang;
		//echo $tong_so_sp.$so_luong;exit;
	}
/***********************************************************************************
* Ham: string xuat_phan_trang (int _get_page, int so_trang)
* Tra ve danh sach trang theo dinh dang "Trang dau tien  < [cac trang] > Trang cuoi cung"
***********************************************************************************/
	function deleteParam($query_string, $param)
	{
		//////xử lý giữ tham số trên url
		
		if($query_string!='')
		{
			//echo $query_string = preg_replace('/(^'.$param.'=[\da-zA-Z]*&)|(&?'.$param.'=[\da-zA-Z]*)|/', '', $query_string);
			//$query_string = substr($query_string,strstr($query_string,$param));
			$start=strpos($query_string,$param);
			if($start!="")
				$query_string=substr($query_string,0,$start);
			else
				$query_string=substr($query_string,0,-5);
			if($query_string)
				$query_string = "$query_string";

		}
		//////xử lý giữ tham số trên url
		return $query_string;
	}

	function xuat_phan_trang($_get_page,$tong_so_trang)
	{
		$page_list = "";
		
		//$query_string = $this->deleteParam($_SERVER['QUERY_STRING'], 'page');
		$query_string = $this->deleteParam($_SERVER['REQUEST_URI'], '-trang');
		//echo '<pre>';
	 	//print_r($_SERVER);
	 	//echo '</rep>';exit;
		if (($_get_page != 1) && ($_get_page))
		{
			$page_list .= "<a class='number' href=\"".$query_string."-trang-1.html\" title=\"Trang đầu\">frist</a> ";
			//$page_list .= " <a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=1\" title=\"Trang đầu\">frist</a> ";
		}
	
		if (($_get_page-1) > 0)
		{
			$page_list .= "<a class='number' href=\"".$query_string."-trang-".($_get_page-1).".html"."\" title=\"Về trang trước\">back</a> ";
			//$page_list .= "<a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=".($_get_page-1)."\" title=\"Về trang trước\">back</a> ";
		}
		
		$vt_dau=max($_get_page-2,1);
		$vt_cuoi=min($_get_page+2,$tong_so_trang);
		//echo $tong_so_trang;
		//exit;
		
		/*if($vt_cuoi==3 && $tong_so_trang>=5) $vt_cuoi=$vt_cuoi+2;
		if(($vt_cuoi==3 && $tong_so_trang==4) || ($vt_cuoi==4 && $tong_so_trang>=5)) $vt_cuoi=$vt_cuoi+1;
		
		if($vt_cuoi==$tong_so_trang && $vt_dau>=3 && $tong_so_trang>5) $vt_dau=$vt_cuoi-5;
		if(($vt_cuoi==$tong_so_trang-1 && $vt_dau>=2 && $tong_so_trang>4) || ($vt_cuoi==$tong_so_trang && $vt_dau>=2 && $tong_so_trang>4)) $vt_dau=$vt_cuoi-10;*/
		
		if($vt_dau>1)	$page_list .= "<a class='number current'>".'... '."</a>";
		
		/* In ra danh sach cac trang va lam cho trang hien tai dam hon va mat link o chan*/		
		for ($i=$vt_dau; $i<=$vt_cuoi; $i++)
		{
			if ($i == $_get_page)
			{
				$page_list .= "<a class='number current'>"."[<strong>$i</strong>]"."</a>";
			}
			else
			{
				//$page_list .= "<a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=".$i."\" title=\"Trang ".$i."\">".$i."</a>";
				$page_list .= "<a class='number' href=\"".$query_string."-trang-".$i.".html"."\" title=\"Trang ".$i."\">".$i."</a>";
			}
			$page_list .= " ";
		}
		
		if($vt_cuoi<$tong_so_trang)		$page_list .= "<a class='number current'>".'... '."</a>";

		/* In link cua trang tiep theo va trang cuoi cung neu can*/
		if (($_get_page+1) <= $tong_so_trang)
		{
			//$page_list .= "<a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=".($_get_page+1)."\" title=\"Đến trang sau\">next</a> ";
			$page_list .= "<a class='number' href=\"".$query_string."-trang-".($_get_page+1).".html"."\" title=\"Đến trang sau\">next</a> ";
		}
		
		if (($_get_page != $tong_so_trang) && ($tong_so_trang != 0))
		{
			$page_list .= "<a class='number' href=\"".$query_string."-trang-".$tong_so_trang.".html"."\" title=\"Trang cuối\">end</a> ";
			//$page_list .= "<a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=".$tong_so_trang."\" title=\"Trang cuối\">end</a> ";
		}
		$page_list .= "</td>\n";

	return $page_list;
	}

	function xuat_phan_trang_admin($_get_page,$tong_so_trang)
	{
		$page_list = "";
		
		$query_string = $this->deleteParam($_SERVER['QUERY_STRING'], 'page');
				
		/* In trang dau tien va nhung link toi trang truoc neu can */
		if (($_get_page != 1) && ($_get_page))
		{
			$page_list .= " <a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=1\" title=\"Trang đầu\">frist</a> ";
		}
	
		if (($_get_page-1) > 0)
		{
			$page_list .= "<a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=".($_get_page-1)."\" title=\"Về trang trước\">back</a> ";
		}
		
		$vt_dau=max($_get_page-2,1);
		$vt_cuoi=min($_get_page+2,$tong_so_trang);
		
		/*if($vt_cuoi==3 && $tong_so_trang>=5) $vt_cuoi=$vt_cuoi+2;
		if(($vt_cuoi==3 && $tong_so_trang==4) || ($vt_cuoi==4 && $tong_so_trang>=5)) $vt_cuoi=$vt_cuoi+1;
		
		if($vt_cuoi==$tong_so_trang && $vt_dau>=3 && $tong_so_trang>5) $vt_dau=$vt_cuoi-5;
		if(($vt_cuoi==$tong_so_trang-1 && $vt_dau>=2 && $tong_so_trang>4) || ($vt_cuoi==$tong_so_trang && $vt_dau>=2 && $tong_so_trang>4)) $vt_dau=$vt_cuoi-10;*/
		
		if($vt_dau>1)	$page_list .= "<a class='number current'>".'... '."</a>";
		
		/* In ra danh sach cac trang va lam cho trang hien tai dam hon va mat link o chan*/		
		for ($i=$vt_dau; $i<=$vt_cuoi; $i++)
		{
			if ($i == $_get_page)
			{
				$page_list .= "<a class='number current'>"."<strong>$i</strong>"."</a>";
			}
			else
			{
				$page_list .= "<a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=".$i."\" title=\"Trang ".$i."\">".$i."</a>";
			}
			$page_list .= " ";
		}
		
		if($vt_cuoi<$tong_so_trang)		$page_list .= "<a class='number current'>".'... '."</a>";

		/* In linh cua trang tiep theo va trang cuoi cung neu can*/
		if (($_get_page+1) <= $tong_so_trang)
		{
			$page_list .= "<a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=".($_get_page+1)."\" title=\"Đến trang sau\">next</a> ";
		}
		
		if (($_get_page != $tong_so_trang) && ($tong_so_trang != 0))
		{
			$page_list .= "<a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=".$tong_so_trang."\" title=\"Trang cuối\">end</a> ";
		}
		$page_list .= "</td>\n";

	return $page_list;
	}

function xuat_phan_trang_hinh($_get_page,$tong_so_trang)
	{
		//$hinh="<a class='number' img src='../hinh_anh/next.png' border='0'";
		$page_list = "";
		
		$query_string = $this->deleteParam($_SERVER['QUERY_STRING'], 'page');
				
		/* In trang dau tien va nhung link toi trang truoc neu can */
		if (($_get_page != 1) && ($_get_page))
		{
			$page_list .= " <a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=1\" title=\"Trang đầu\"><img src='../hinh_anh/first.png' border='0'/></a> ";
		}
	
		if (($_get_page-1) > 0)
		{
			$page_list .= "<a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=".($_get_page-1)."\" title=\"Về trang trước\"><img src='../hinh_anh/back.png' border='0'/></a> ";
		}
		
		$vt_dau=max($_get_page-2,1);
		$vt_cuoi=min($_get_page+2,$tong_so_trang);
		
		if($vt_cuoi==3) $vt_cuoi=$vt_cuoi+2;
		if($vt_cuoi==4) $vt_cuoi=$vt_cuoi+1;
		
		if($vt_dau==$tong_so_trang-2) $vt_dau=$vt_dau-2;
		if($vt_dau==$tong_so_trang-3) $vt_dau=$vt_dau-1;
		
		if($vt_dau>1)	$page_list .= "<b>".'... '."</b>";
		
		/* In ra danh sach cac trang va lam cho trang hien tai dam hon va mat link o chan*/		
		for ($i=$vt_dau; $i<=$vt_cuoi; $i++)
		{
			if ($i == $_get_page)
			{
				$page_list .= "<a class='number current'>"."[<strong>$i</strong>]"."</a>";
			}
			else
			{
				$page_list .= "<a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=".$i."\" title=\"Trang ".$i."\">".$i."</a>";
			}
			$page_list .= " ";
		}
		
		if($vt_cuoi<$tong_so_trang)   $page_list .= "<a class='number current'>".'... '."</a>";

		/* In linh cua trang tiep theo va trang cuoi cung neu can*/
		if (($_get_page+1) <= $tong_so_trang)
		{
			$page_list .= "<a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=".($_get_page+1)."\" title=\"Đến trang sau\"><img src='../hinh_anh/next.PNG' border='0'/></a> ";
		}
		
		if (($_get_page != $tong_so_trang) && ($tong_so_trang != 0))
		{
			$page_list .= "<a class='number' href=\"".$_SERVER['PHP_SELF']."?".$query_string."page=".$tong_so_trang."\" title=\"Trang cuối\"><img src='../hinh_anh/end.PNG' border='0'/></a> ";
		}
		$page_list .= "</td>\n";

	return $page_list;
	}
}
?>